'''
Created on Jun 6, 2012

@author: thomas_dao
'''
from google.appengine.ext import ndb
from entity.entity import Photo


@ndb.tasklet
def get_all(**q_options):
    qry = Photo.query().order(-Photo.created_time)
    data = yield qry.fetch_async(20, **q_options)
    raise ndb.Return(data)


@ndb.tasklet
def get_by_author(author_key, **q_options):
    qry = Photo.query(Photo.author == author_key).order(-Photo.created_time)
    data = yield qry.fetch_async(20, **q_options)
    raise ndb.Return(data)
           
@ndb.tasklet
def get_by_album(album_key, **q_options):
    """
    Return all photos in album
    """
    qry = Photo.query(Photo.album == album_key).order(-Photo.created_time)
    data = yield qry.fetch_async(**q_options)
    raise ndb.Return(data)
        
@ndb.tasklet
def get_by_location(location_key, **q_options):
    qry = Photo.query(Photo.location == location_key).order(-Photo.created_time)
    data = yield qry.fetch_async(20, **q_options)
    raise ndb.Return(data)
    
@ndb.tasklet
def get_cover_photos_by_event(event_key, **q_options):
    """
    Return all photos in album
    """
    event = yield event_key.get_async()
    qry = Photo.query(Photo.album == event.cover_album).order(-Photo.created_time)
    data = yield qry.fetch_async(**q_options)
    raise ndb.Return(data)


@ndb.tasklet
def get_default_photos_by_event(event_key, **q_options):
    """
    Return all photos in album
    """
    event = yield event_key.get_async()
    qry = Photo.query(Photo.album == event.default_album).order(-Photo.created_time)
    data = yield qry.fetch_async(**q_options)
    raise ndb.Return(data)
